<template>
  <el-container class="main-layout">
    <el-aside width="240px" class="sidebar">
      <div class="logo">
        <div class="logo-icon">🧩</div>
        <div class="logo-text">
          <h2>孤独症干预平台</h2>
          <p class="logo-subtitle">用爱点亮星星</p>
        </div>
      </div>
      <div class="menu-divider"></div>
      <el-menu
        :default-active="activeMenu"
        class="sidebar-menu"
        router
        background-color="transparent"
        text-color="#4a5568"
        active-text-color="#ffb3d9"
      >
        <el-menu-item
          v-for="route in menuRoutes"
          :key="route.path"
          :index="'/app/' + route.path"
          class="custom-menu-item"
        >
          <el-icon class="menu-icon"><component :is="route.meta.icon" /></el-icon>
          <span class="menu-text">{{ route.meta.title }}</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <el-container class="right-container">
      <el-header class="header">
        <div class="header-left">
          <span class="welcome-text">欢迎，{{ userInfo?.phone || '用户' }}</span>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-dropdown">
              <el-icon><User /></el-icon>
              <span>我的账号</span>
              <el-icon class="el-icon--right"><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

const userInfo = computed(() => userStore.userInfo)

const menuRoutes = computed(() => {
  return router.options.routes
    .find(r => r.path === '/app')
    ?.children.filter(child => child.meta?.title && !child.meta?.hideInMenu) || []
})

const activeMenu = computed(() => route.path)

const handleCommand = (command) => {
  if (command === 'logout') {
    userStore.logout()
  }
}
</script>

<style scoped>
.main-layout {
  min-height: 100vh;
}

.sidebar {
  background: linear-gradient(180deg, #fff5fa 0%, #ffffff 100%);
  box-shadow: 4px 0 12px rgba(255, 179, 217, 0.1);
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  height: 100vh;
  overflow-y: auto;
  z-index: 1000;
}

.sidebar::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 3px;
  height: 100%;
  background: linear-gradient(180deg, #ffb3d9 0%, #ffc299 100%);
}

.right-container {
  margin-left: 240px;
  width: calc(100% - 240px);
}

.logo {
  height: auto;
  padding: 24px 16px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 12px;
  background: linear-gradient(135deg, #ffb3d9 0%, #ffc299 100%);
  border-radius: 0 0 20px 20px;
  margin-bottom: 8px;
}

.logo-icon {
  font-size: 40px;
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0px); }
  50% { transform: translateY(-5px); }
}

.logo-text {
  flex: 1;
}

.logo h2 {
  color: #fff;
  font-size: 20px;
  margin: 0;
  font-weight: bold;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.logo-subtitle {
  color: rgba(255, 255, 255, 0.95);
  font-size: 13px;
  margin: 4px 0 0 0;
  font-weight: 500;
}

.menu-divider {
  height: 2px;
  background: linear-gradient(90deg, transparent 0%, #ffb3d9 50%, transparent 100%);
  margin: 16px 0;
  opacity: 0.3;
}

.sidebar-menu {
  border-right: none;
  padding: 0 12px;
}

.custom-menu-item {
  margin-bottom: 8px;
  border-radius: 12px;
  transition: all 0.3s ease;
  font-size: 16px;
  height: 50px;
  line-height: 50px;
}

.custom-menu-item:hover {
  background: linear-gradient(90deg, #fff0f8 0%, #ffe8f0 100%) !important;
  transform: translateX(5px);
  box-shadow: 0 2px 8px rgba(255, 179, 217, 0.2);
}

.custom-menu-item.is-active {
  background: linear-gradient(90deg, #ffb3d9 0%, #ffc299 100%) !important;
  color: #fff !important;
  box-shadow: 0 4px 12px rgba(255, 179, 217, 0.4);
}

.custom-menu-item.is-active .menu-icon,
.custom-menu-item.is-active .menu-text {
  color: #fff !important;
}

.menu-icon {
  font-size: 20px;
  margin-right: 8px;
}

.menu-text {
  font-weight: 500;
}

.header {
  background: linear-gradient(90deg, #ffffff 0%, #fff9fc 100%);
  box-shadow: 0 2px 8px rgba(255, 179, 217, 0.15);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  border-bottom: 2px solid #ffe8f0;
}

.welcome-text {
  font-size: 16px;
  color: #606266;
  font-weight: 500;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 20px;
  transition: all 0.3s;
  background: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  font-size: 15px;
}

.user-dropdown:hover {
  background: linear-gradient(90deg, #ffb3d9 0%, #ffc299 100%);
  color: #fff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(255, 179, 217, 0.3);
}

.main-content {
  background-color: #f5f7fa;
  padding: 24px;
}
</style>
